<?php

namespace App\Console\Commands\OpenShop;

use App\Enums\Check\CheckEnum;
use App\Enums\OpenShop\InvestmentPromotion\SignOrderEnum;
use App\Enums\OpenShop\InvestmentPromotion\SiteMissionEnum;
use App\Enums\OpenShop\MissionTemplateEnum;
use App\Library\Guanyuan\GuanyuanTool;
use App\Models\Check\CheckCmbBankFlow;
use App\Models\College\CollegeTrainingOrderModel;
use App\Models\Cruise\CruiseOrderModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopMainProcessModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopOrderTagsMappingModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderPayHistoryModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderPermissionAnchorModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSiteMissionModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSpaceDesignCostMappingModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSpaceDesignSoftOutfitMappingModel;
use App\Models\OpenShop\SiteLocation\OpenShopSiteLocationModel;
use App\Models\OpenShop\StartBusiness\OpenShopStartBusinessModel;
use App\Remote\Guanyuan\GuanyuanRemote;
use App\Remote\Orm\OrmNewOrderRemote;
use App\Service\College\CollegeTrainOrderService;
use App\Service\OpenShop\Feishu\KfosService;
use App\Service\OpenShop\Feishu\MessageSendService;
use App\Service\OpenShop\Feishu\MessageService;
use App\Service\OpenShop\Feishu\OrganizationManageService;
use App\Service\OpenShop\Feishu\ShopTagsService;
use App\Service\OpenShop\User\RmsUserService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Config;

class RefreshOrderDataFromGyCloseShop extends Command
{
    protected $signature   = 'openShop:refresh-order-data-from-close-shop';
    protected $description = '刷新订单数据';

    public function handle()
    {
        $field = [
            '开业日期' => 'startBusinessTime',
            '铺位号'   => 'shopCode'
        ];

        $data = GuanyuanTool::fetch('d4e637ebdbb154560ad54721', 'c636d049c8ad6475987065db', [], 8, $field);

        $data = array_filter($data, function ($item) {
            return !empty($item['startBusinessTime']);
        });

        $out = '开店订单,门店编号,观远开业时间,开店系统开业时间' . PHP_EOL;
        foreach ($data as $datum) {
            $orderInfo = OpenShopSignOrderModel::query()
                                               ->where('shopCode', $datum['shopCode'])
                                               ->orderBy('mainProcessId', 'desc')
                                               ->first();

            if (!$orderInfo) {
                dump($datum['shopCode'] . '门店编号在开店系统中不存在');
                continue;
            }

            $orgStartBusinessTime = OpenShopStartBusinessModel::query()->where('orderNo', $orderInfo->orderNo)
                                                              ->value('startBusinessTime');

            if (!$orgStartBusinessTime) {
                continue;
            }

            // 判断是否一致
            $orgStartBusinessTimeData = date('Y-m-d', $orgStartBusinessTime);
            if ($orgStartBusinessTimeData != $datum['startBusinessTime']) {

                $out .= vsprintf('%s,%s,%s,%s', [
                        $orderInfo->orderNo,
                        $datum['shopCode'],
                        $datum['startBusinessTime'],
                        $orgStartBusinessTimeData
                    ]) . PHP_EOL;
            }
        }
        file_put_contents('./refresh-order-data-from-close-shop.csv', $out);
    }


}
